//
//  AppDelegate.swift
//  Weibo
//
//  Created by gdarkness on 16/3/20.
//  Copyright © 2016年 gdarkness. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        setAppearance()
        
        //测试加载账号
        let userAccount = UserAccountViewModel.shareInstance.userAccount
        print("AppDelegate userAccount: \(userAccount)")
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        
        //let taBarControl = MainViewController()
        
        window?.rootViewController = defaultViewController()
        //window?.rootViewController?.view.backgroundColor = UIColor.whiteColor()
        
        window?.makeKeyAndVisible()
        return true
    }
    
    
    private func defaultViewController() -> UIViewController {
        if !UserAccountViewModel.shareInstance.userLogin {
            //没有登录
            return MainViewController()
        }else{
            
            return isNewVersion() ? NewFeautreViewController() : WelcomeViewController()
        }
        //登录过了
    }
    
    //界面切换统一调用这个方法，到时候如果界面切换出现问题，直接在这个方法打断点，方便调试
    //外部使用需要拿到对象在调用，比较麻烦
    func switchRootViewController(controller: UIViewController) {
        window?.rootViewController = controller
    }
    
    //外部调用切换跟控制器方法
    class func outSwitchRootViewController(controller: UIViewController) {
        let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
            appDelegate?.switchRootViewController(controller)
    }
   
    //是否新版本
    private func isNewVersion() ->Bool {
        //获取当前版本
        let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        print("version: \(version)")
        
        //获取上一次版本
        let sandboxKey = "sandboxKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxKey)
        print("sandboxVersion: \(sandboxVersion)")
        
        //对比，不一样就是新版本
        let newVersion = version != sandboxVersion
        
        //是新版本好。保存当前版本号
        if newVersion{
            NSUserDefaults.standardUserDefaults().setObject(version, forKey: sandboxKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        return newVersion
    }
    
    private func setAppearance() {
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

